package com.cli.springboottemplateengine.controller;

import com.cli.springboottemplateengine.model.TemplateRenderRequest;
import com.cli.springboottemplateengine.service.TemplateRenderService;
import org.springframework.http.ResponseEntity;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;

@CrossOrigin("*")
@RestController
@RequestMapping("/template")
public class TemplateRenderController {

    private final TemplateRenderService renderService;

    public TemplateRenderController(TemplateRenderService renderService) {
        this.renderService = renderService;
    }

    @PostMapping("/render")
    public ResponseEntity<?> render(@RequestBody TemplateRenderRequest request) {
        String result = renderService.render(request.getType(), request.getTemplate(), request.getParams());
        return ResponseEntity.ok(Collections.singletonMap("result", result));
    }
}

